import { BadRequestException } from '@nestjs/common';
import { Request, Express } from 'express';
import { FileFilterCallback } from 'multer';

/**
 * 创建文件上传过滤器
 */
export const createFileFilter = (fileTypes: string[]) => {
  return (request: Request, file: Express.Multer.File, callback: FileFilterCallback) => {
    const allowed = fileTypes.some(type => type === file.mimetype)
    if (allowed) {
      callback(null, true)
    } else {
      callback(new BadRequestException('不支持上传此类型文件'))
    }
  }
}


/**
 * 图像文件过滤器
 */
export const imageFileFilter = createFileFilter([
  "image/jpeg",
  "image/png",
  "image/jpg",
])